<?php

namespace App\Admin\Http\Controllers\Rbac;

use Flash;
use Hash;
use Illuminate\Http\Request;
use App\Admin\Eloquent\User;
use App\Admin\Eloquent\Role;
use App\Admin\Eloquent\RoleUser;
use App\Admin\Http\Controllers\Controller;
use App\Extensions\Pagination\BootstrapThreePresenter;

/**
 * 管理员控制器
 */
class UsersController extends Controller
{
    /**
     * 查询管理员信息列表
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = $request->only('search');

        $users = User::paginateQueryAdministor($query);

        // 追加分页查询条件
        if (!empty($query)) {
            $users->appends($query);
        }

        // 分页
        $links = (new BootstrapThreePresenter($users))->render();

        return view('admin.rbac.users.index', compact('users', 'links', 'query'));
    }

    /**
     * 新增管理员信息表单
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $roles=Role::get();
        return view('admin.rbac.users.create',compact('roles'));
    }

    /**
     * 新增管理员信息
     *
     * @param  \App\Admin\Http\Requests\AdministorRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validateUser($request);

        $user = new User($request->input());

        $user->scope = User::USER_SCOPE_ADMINISTOR;

        // 密码加密
        $user->password = Hash::make( md5($request->input('password')) );

        if (!$user->save()) {
            return back()
                ->withErrors($user->getErrors())
                ->withInput();
        }
        $roles=$request->input('role');
        $role_data=array();
        foreach ($roles as $value) {
            $role_data[]=array('user_id'=>$user->id,'role_id'=>$value);
        }
        RoleUser::insert($role_data);

        Flash::success('新增管理员信息成功');

        return $this->redirectAction('Rbac\UsersController@index');
    }

    /**
     * 编辑管理员信息表单
     *
     * @param  mixed  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = $this->user($id);
        $roles=Role::get();
        $user_role=RoleUser::where('user_id',$user->id)->lists('role_id')->toArray();
        return view('admin.rbac.users.edit', compact('user','roles','user_role'));
    }

    /**
     * 编辑管理员信息
     *
     * @param  mixed  $id
     * @param  Request  $request
     * @return \Illuminate\Http\Response
     */
    public function update($id, Request $request)
    {
        $this->validateUser($request, $id);

        $user = $this->user($id);

        $user->password = Hash::make( md5($request->input('password')) );

        $user->save();

        RoleUser::where('user_id',$user->id)->delete();
        $roles=$request->input('role');
        $role_data=array();
        foreach ($roles as $value) {
            $role_data[]=array('user_id'=>$user->id,'role_id'=>$value);
        }
        RoleUser::insert($role_data);

        Flash::success('编辑管理员信息成功');

        return $this->redirectAction('Rbac\UsersController@index');
    }

    /**
     * 删除管理员信息
     *
     * @param  mixed  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        $user = $this->user($id);

        if ($this->currentUser()->username == $user->username) {

            Flash::error('删除失败, 不能删除用户自己');

        } else {
            $user->delete();

            Flash::success('删除管理员信息成功');
        }

        return $this->redirectAction('Rbac\UsersController@index');
    }

    /**
     * 系统管理员
     *
     * @param  mixed $id
     * @return \App\Eloquent\Administor
     *
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    protected function user($id)
    {
        return User::administors()->findOrFail($id);
    }

    protected function validateUser(Request $request)
    {
        $this->validate($request, [
            'password' => 'required|min:6|max:60|confirmed',
        ]);
    }
}
